package org.dromara.timemate.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.ValidatorUtils;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.core.validate.QueryGroup;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.SysUser;
import org.dromara.system.domain.vo.SysUserVo;
import org.dromara.system.service.ISysUserService;
import org.dromara.timemate.domain.DeviceInfo;
import org.dromara.timemate.domain.TestDemo;
import org.dromara.timemate.domain.bo.DeviceManageBo;
import org.dromara.timemate.domain.bo.TestDemoBo;
import org.dromara.timemate.domain.bo.TestDemoImportVo;
import org.dromara.timemate.domain.vo.DeviceManageVo;
import org.dromara.timemate.domain.vo.TestDemoVo;
import org.dromara.timemate.service.DeviceManageService;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController
@RequestMapping("/tm/device")
public class DeviceController extends BaseController {
    @Resource
    private DeviceManageService deviceManageService;

    /**
     * 设备管理分页查询（电话号码、设备类型）
     *
     * @param bo
     * @param pageQuery
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo<DeviceManageVo> list(@Validated(QueryGroup.class) DeviceManageBo bo, PageQuery pageQuery) {
        return  deviceManageService.queryPageList(bo, pageQuery);
    }

    /**
     * 登陆后，通过tenantId（租户id），查询出该租户下的所有用户的userIds和phoneNumber，供用户选择
     * @return
     */
    @GetMapping
    public R<List<DeviceManageVo>> ByTidGetUserVo() {
        List<DeviceManageVo> deviceManageVos = deviceManageService.queryByTenantId();
        return R.ok(deviceManageVos);
    }


    /**
     * 添加设备信息
     * @param deviceManageBo：userId（必填）、wearerId、deviceType（必填）、deviceName
     * @return
     */
    @PostMapping("/add")
    public R<Void> addDeviceManageInfo(@Valid @RequestBody DeviceManageBo deviceManageBo){
        ValidatorUtils.validate(deviceManageBo, AddGroup.class);
        return toAjax(deviceManageService.addDeviceManageInfo(deviceManageBo));
    }

    /**
     * 修改设备信息
     * @param deviceManageBo(id,deviceType,deviceName)必填
     * @return
     */
    @RepeatSubmit
    @PutMapping
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DeviceManageBo deviceManageBo) {
        return toAjax(deviceManageService.updateByBo(deviceManageBo));
    }

    /**
     * 通过上传设备id，批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/batchDelete")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @RequestBody List<Long> ids) {
        return toAjax(deviceManageService.deleteWithValidByIds(ids));
    }

/*    *//**
     * 导出设备信息
     * @param bo
     * @param response
     *//*
    @PostMapping("/export")
    public void export(@Validated DeviceManageBo bo, HttpServletResponse response) {
        List<DeviceManageVo> list = deviceManageService.queryList(bo);
        ExcelUtil.exportExcel(list, "设备信息表", DeviceManageVo.class, response);
    }

    *//**
     * 导入设备信息,还未测试
     * @param file
     * @return
     * @throws Exception
     *//*
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file) throws Exception {
        ExcelResult<DeviceManageVo> excelResult = ExcelUtil.importExcel(file.getInputStream(), DeviceManageVo.class, true);
        List<DeviceInfo> list = MapstructUtils.convert(excelResult.getList(), DeviceInfo.class);
        deviceManageService.saveBatch(list);
        return R.ok(excelResult.getAnalysis());
    }*/


}
